# encoding: utf-8
require File.expand_path('../solar_terms/version', __FILE__)
require 'date'

module SolarTerms
  # Timezone: +08:00
  TERM_NAMES = ["小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至"]

  TERMS_TABLE = [
    "2012-01-06 06:43:58", "2012-01-21 00:09:52", "2012-02-04 18:22:26", "2012-02-19 14:17:35", "2012-03-05 12:21:02", "2012-03-20 13:14:22",
    "2012-04-04 17:05:37", "2012-04-20 00:12:06", "2012-05-05 10:19:48", "2012-05-20 23:15:37", "2012-06-05 14:25:59", "2012-06-21 07:08:50",
    "2012-07-07 00:40:44", "2012-07-22 18:00:57", "2012-08-07 10:30:36", "2012-08-23 01:06:56", "2012-09-07 13:29:01", "2012-09-22 22:48:58",
    "2012-10-08 05:11:39", "2012-10-23 08:13:27", "2012-11-07 08:25:54", "2012-11-22 05:50:03", "2012-12-07 01:18:57", "2012-12-21 19:11:36",

    "2013-01-05 12:33:42", "2013-01-20 05:51:43", "2013-02-04 00:13:26", "2013-02-18 20:01:32", "2013-03-05 18:14:48", "2013-03-20 19:01:53",
    "2013-04-04 23:02:26", "2013-04-20 06:03:19", "2013-05-05 16:18:10", "2013-05-21 05:09:32", "2013-06-05 20:23:18", "2013-06-21 13:03:59",
    "2013-07-07 06:34:37", "2013-07-22 23:56:00", "2013-08-07 16:20:24", "2013-08-23 07:01:39", "2013-09-07 19:16:14", "2013-09-23 04:44:01",
    "2013-10-08 10:58:26", "2013-10-23 14:09:43", "2013-11-07 14:13:51", "2013-11-22 11:48:07", "2013-12-07 07:08:35", "2013-12-22 01:11:04",

    "2014-01-05 18:24:11", "2014-01-20 11:51:13", "2014-02-04 06:03:09", "2014-02-19 01:59:27", "2014-03-06 00:02:12", "2014-03-21 00:57:07",
    "2014-04-05 04:46:39", "2014-04-20 11:55:33", "2014-05-05 21:59:24", "2014-05-21 10:58:58", "2014-06-06 02:03:01", "2014-06-21 18:51:11",
    "2014-07-07 12:14:50", "2014-07-23 05:41:25", "2014-08-07 22:02:33", "2014-08-23 12:46:01", "2014-09-08 01:01:23", "2014-09-23 10:29:03",
    "2014-10-08 16:47:25", "2014-10-23 19:57:04", "2014-11-07 20:06:40", "2014-11-22 17:38:15", "2014-12-07 13:04:08", "2014-12-22 07:03:03",

    "2015-01-06 00:20:33", "2015-01-20 17:43:14", "2015-02-04 11:58:27", "2015-02-19 07:49:46", "2015-03-06 05:55:41", "2015-03-21 06:45:08",
    "2015-04-05 10:39:12", "2015-04-20 17:41:52", "2015-05-06 03:52:39", "2015-05-21 16:44:46", "2015-06-06 07:58:09", "2015-06-22 00:37:57",
    "2015-07-07 18:12:16", "2015-07-23 11:30:32", "2015-08-08 04:01:25", "2015-08-23 18:37:16", "2015-09-08 06:59:31", "2015-09-23 16:20:29",
    "2015-10-08 22:42:50", "2015-10-24 01:46:43", "2015-11-08 01:58:38", "2015-11-22 23:25:14", "2015-12-07 18:53:19", "2015-12-22 12:47:54",

    "2016-01-06 06:08:23", "2016-01-20 23:27:07", "2016-02-04 17:46:07", "2016-02-19 13:33:48", "2016-03-05 11:43:37", "2016-03-20 12:30:16",
    "2016-04-04 16:27:33", "2016-04-19 23:29:31", "2016-05-05 09:41:54", "2016-05-20 22:36:35", "2016-06-05 13:48:35", "2016-06-21 06:34:19",
    "2016-07-07 00:03:30", "2016-07-22 17:30:17", "2016-08-07 09:53:06", "2016-08-23 00:38:28", "2016-09-07 12:51:06", "2016-09-22 22:21:08",
    "2016-10-08 04:33:24", "2016-10-23 07:45:36", "2016-11-07 07:47:42", "2016-11-22 05:22:23", "2016-12-07 00:41:03", "2016-12-21 18:44:07",

    "2017-01-05 11:55:40", "2017-01-20 05:23:36", "2017-02-03 23:34:04", "2017-02-18 19:31:24", "2017-03-05 17:32:46", "2017-03-20 18:28:38",
    "2017-04-04 22:17:14", "2017-04-20 05:26:53", "2017-05-05 15:30:56", "2017-05-21 04:30:48", "2017-06-05 19:36:33", "2017-06-21 12:24:02",
    "2017-07-07 05:50:38", "2017-07-22 23:15:18", "2017-08-07 15:39:58", "2017-08-23 06:20:12", "2017-09-07 18:38:35", "2017-09-23 04:01:48",
    "2017-10-08 10:22:07", "2017-10-23 13:26:40", "2017-11-07 13:37:47", "2017-11-22 11:04:36", "2017-12-07 06:32:36", "2017-12-22 00:27:54",

    "2018-01-05 17:48:44", "2018-01-20 11:09:01", "2018-02-04 05:28:32", "2018-02-19 01:17:59", "2018-03-05 23:28:10", "2018-03-21 00:15:22",
    "2018-04-05 04:12:42", "2018-04-20 11:12:26", "2018-05-05 21:25:14", "2018-05-21 10:14:30", "2018-06-06 01:28:59", "2018-06-21 18:07:10",
    "2018-07-07 11:41:43", "2018-07-23 05:00:14", "2018-08-07 21:30:32", "2018-08-23 12:08:31", "2018-09-08 00:29:40", "2018-09-23 09:54:06",
    "2018-10-08 16:14:42", "2018-10-23 19:22:16", "2018-11-07 19:31:33", "2018-11-22 17:01:13", "2018-12-07 12:25:42", "2018-12-22 06:22:34",

    "2019-01-05 23:38:52", "2019-01-20 16:59:29", "2019-02-04 11:14:14", "2019-02-19 07:03:52", "2019-03-06 05:09:35", "2019-03-21 05:58:20",
    "2019-04-05 09:51:19", "2019-04-20 16:55:13", "2019-05-06 03:02:43", "2019-05-21 15:59:05", "2019-06-06 07:06:22", "2019-06-21 23:54:11",
    "2019-07-07 17:20:29", "2019-07-23 10:50:17", "2019-08-08 03:13:00", "2019-08-23 18:01:56", "2019-09-08 06:16:49", "2019-09-23 15:50:06",
    "2019-10-08 22:05:35", "2019-10-24 01:19:40", "2019-11-08 01:24:15", "2019-11-22 22:58:49", "2019-12-07 18:18:18", "2019-12-22 12:19:20",

    "2020-01-06 05:30:00", "2020-01-20 22:54:38", "2020-02-04 17:03:17", "2020-02-19 12:56:58", "2020-03-05 10:56:50", "2020-03-20 11:49:34",
    "2020-04-04 15:38:10", "2020-04-19 22:45:29", "2020-05-05 08:51:29", "2020-05-20 21:49:18", "2020-06-05 12:58:25", "2020-06-21 05:43:37",
    "2020-07-06 23:14:24", "2020-07-22 16:36:52", "2020-08-07 09:06:08", "2020-08-22 23:44:54", "2020-09-07 12:07:53", "2020-09-22 21:30:32",
    "2020-10-08 03:55:07", "2020-10-23 06:59:28", "2020-11-07 07:13:55", "2020-11-22 04:39:46", "2020-12-07 00:09:35", "2020-12-21 18:02:21",

    "2021-01-05 11:23:29", "2021-01-20 04:39:48", "2021-02-03 22:58:46", "2021-02-18 18:43:52", "2021-03-05 16:53:38", "2021-03-20 17:37:25",
    "2021-04-04 21:35:07", "2021-04-20 04:33:24", "2021-05-05 14:47:11", "2021-05-21 03:37:06", "2021-06-05 18:52:01", "2021-06-21 11:32:06",
    "2021-07-07 05:05:22", "2021-07-22 22:26:22", "2021-08-07 14:53:56", "2021-08-23 05:34:56", "2021-09-07 17:52:57", "2021-09-23 03:21:01",
    "2021-10-08 09:39:01", "2021-10-23 12:51:04", "2021-11-07 12:58:46", "2021-11-22 10:33:44", "2021-12-07 05:57:09", "2021-12-21 23:59:24",

    "2022-01-05 17:14:05", "2022-01-20 10:39:05", "2022-02-04 04:50:40", "2022-02-19 00:42:57", "2022-03-05 22:43:39", "2022-03-20 23:33:23",
    "2022-04-05 03:20:10", "2022-04-20 10:24:14", "2022-05-05 20:25:51", "2022-05-21 09:22:29", "2022-06-06 00:25:43", "2022-06-21 17:13:46",
    "2022-07-07 10:38:00", "2022-07-23 04:06:57", "2022-08-07 20:29:06", "2022-08-23 11:16:05", "2022-09-07 23:32:11", "2022-09-23 09:03:38",
    "2022-10-08 15:22:22", "2022-10-23 18:35:42", "2022-11-07 18:45:26", "2022-11-22 16:20:27", "2022-12-07 11:46:10", "2022-12-22 05:48:05",

    "2023-01-05 23:04:42", "2023-01-20 16:29:23", "2023-02-04 10:42:25", "2023-02-19 06:34:08", "2023-03-06 04:36:08", "2023-03-21 05:24:18",
    "2023-04-05 09:13:00", "2023-04-20 16:13:30", "2023-05-06 02:18:41", "2023-05-21 15:09:02", "2023-06-06 06:18:13", "2023-06-21 22:57:44",
    "2023-07-07 16:30:36", "2023-07-23 09:50:29", "2023-08-08 02:22:50", "2023-08-23 17:01:13", "2023-09-08 05:26:35", "2023-09-23 14:49:50",
    "2023-10-08 21:15:31", "2023-10-24 00:20:47", "2023-11-08 00:35:33", "2023-11-22 22:02:35", "2023-12-07 17:32:51", "2023-12-22 11:27:12",

    "2024-01-06 04:49:14", "2024-01-20 22:07:12", "2024-02-04 16:27:01", "2024-02-19 12:13:05", "2024-03-05 10:22:40", "2024-03-20 11:06:20",
    "2024-04-04 15:02:11", "2024-04-19 21:59:43", "2024-05-05 08:09:57", "2024-05-20 20:59:26", "2024-06-05 12:09:44", "2024-06-21 04:50:52",
    "2024-07-06 22:19:53", "2024-07-22 15:44:13", "2024-08-07 08:09:07", "2024-08-22 22:54:50", "2024-09-07 11:11:13", "2024-09-22 20:43:34",
    "2024-10-08 02:59:51", "2024-10-23 06:14:41", "2024-11-07 06:19:55", "2024-11-22 03:56:21", "2024-12-06 23:16:48", "2024-12-21 17:20:22",

    "2025-01-05 10:32:33", "2025-01-20 03:59:57", "2025-02-03 22:10:19", "2025-02-18 18:06:30", "2025-03-05 16:07:11", "2025-03-20 17:01:23",
    "2025-04-04 20:48:25", "2025-04-20 03:55:52", "2025-05-05 13:57:06", "2025-05-21 02:54:33", "2025-06-05 17:56:30", "2025-06-21 10:42:09",
    "2025-07-07 04:04:56", "2025-07-22 21:29:20", "2025-08-07 13:51:27", "2025-08-23 04:33:44", "2025-09-07 16:51:49", "2025-09-23 02:19:16",
    "2025-10-08 08:41:07", "2025-10-23 11:50:52", "2025-11-07 12:03:56", "2025-11-22 09:35:26", "2025-12-07 05:04:24", "2025-12-21 23:02:53",

    "2026-01-05 16:22:58", "2026-01-20 09:44:46", "2026-02-04 04:02:00", "2026-02-18 23:51:46", "2026-03-05 21:58:51", "2026-03-20 22:45:46",
    "2026-04-05 02:39:50", "2026-04-20 09:38:57", "2026-05-05 19:48:34", "2026-05-21 08:36:37", "2026-06-05 23:48:11", "2026-06-21 16:24:22",
    "2026-07-07 09:56:47", "2026-07-23 03:12:57", "2026-08-07 19:42:32", "2026-08-23 10:18:38", "2026-09-07 22:41:07", "2026-09-23 08:05:05",
    "2026-10-08 14:29:09", "2026-10-23 17:37:44", "2026-11-07 17:51:51", "2026-11-22 15:23:05", "2026-12-07 10:52:20", "2026-12-22 04:50:03",

    "2027-01-05 22:09:50", "2027-01-20 15:29:41", "2027-02-04 09:46:08", "2027-02-19 05:33:18", "2027-03-06 03:39:19", "2027-03-21 04:24:32",
    "2027-04-05 08:17:19", "2027-04-20 15:17:33", "2027-05-06 01:25:04", "2027-05-21 14:18:09", "2027-06-06 05:25:41", "2027-06-21 22:10:40",
    "2027-07-07 15:36:55", "2027-07-23 09:04:27", "2027-08-08 01:26:36", "2027-08-23 16:14:07", "2027-09-08 04:28:17", "2027-09-23 14:01:32",
    "2027-10-08 20:16:54", "2027-10-23 23:32:40", "2027-11-07 23:38:20", "2027-11-22 21:16:01", "2027-12-07 16:37:26", "2027-12-22 10:42:00",

    "2028-01-06 03:54:26", "2028-01-20 21:21:49", "2028-02-04 15:31:00", "2028-02-19 11:25:50", "2028-03-05 09:24:31", "2028-03-20 10:16:52",
    "2028-04-04 14:02:51", "2028-04-19 21:09:16", "2028-05-05 07:12:03", "2028-05-20 20:09:37", "2028-06-05 11:15:49", "2028-06-21 04:01:46",
    "2028-07-06 21:30:06", "2028-07-22 14:53:49", "2028-08-07 07:20:58", "2028-08-22 22:00:44", "2028-09-07 10:21:53", "2028-09-22 19:45:06",
    "2028-10-08 02:08:16", "2028-10-23 05:13:13", "2028-11-07 05:27:06", "2028-11-22 02:54:14", "2028-12-06 22:24:31", "2028-12-21 16:19:28",

    "2029-01-05 09:41:43", "2029-01-20 03:00:37", "2029-02-03 21:20:31", "2029-02-18 17:07:36", "2029-03-05 15:17:20", "2029-03-20 16:01:40",
    "2029-04-04 19:58:09", "2029-04-20 02:55:28", "2029-05-05 13:07:33", "2029-05-21 01:55:40", "2029-06-05 17:09:43", "2029-06-21 09:48:06",
    "2029-07-07 03:22:11", "2029-07-22 20:41:58", "2029-08-07 13:11:36", "2029-08-23 03:51:27", "2029-09-07 16:11:43", "2029-09-23 01:38:15",
    "2029-10-08 07:57:54", "2029-10-23 11:07:51", "2029-11-07 11:16:35", "2029-11-22 08:49:13", "2029-12-07 04:13:42", "2029-12-21 22:13:59",

    "2030-01-05 15:30:20", "2030-01-20 08:54:06", "2030-02-04 03:08:05", "2030-02-18 22:59:38", "2030-03-05 21:02:58", "2030-03-20 21:51:51",
    "2030-04-05 01:40:44", "2030-04-20 08:43:21", "2030-05-05 18:46:03", "2030-05-21 07:40:50", "2030-06-05 22:44:15", "2030-06-21 15:31:03",
    "2030-07-07 08:55:16", "2030-07-23 02:24:38", "2030-08-07 18:47:06", "2030-08-23 09:36:06", "2030-09-07 21:52:31", "2030-09-23 07:26:34",
    "2030-10-08 13:44:54", "2030-10-23 17:00:16", "2030-11-07 17:08:23", "2030-11-22 14:44:20"
  ]

  def solar_term
    index_of_all_terms % 24
  end

  def solar_term_name
    TERM_NAMES[solar_term]
  end

  def solar_term_time
    solar_term_times[index_of_all_terms]
  end

  def solar_term_seconds
    self.to_time.utc.to_i - solar_term_time.to_i
  end

  def solar_term_minutes
    solar_term_seconds / 60.0
  end

  def solar_term_hours
    solar_term_seconds / 3600.0
  end

  def solar_term_days
    solar_term_seconds / 86400.0
  end

  private
  def index_of_all_terms
    t = self.to_time
    v = solar_term_times.select{|time| time >= t.utc }.first
    v == t.utc ? solar_term_times.index(v) : solar_term_times.index(v) - 1
  end

  def solar_term_times
    @solar_term_times ||= TERMS_TABLE.collect{|s| args = s.split(/[^\d]+/).collect{|n| n.to_i }.push "+08:00"; Time.new(*args).utc }
  end
end

Date.class_eval { include ::SolarTerms }
Time.class_eval { include ::SolarTerms }
DateTime.class_eval { include ::SolarTerms }